package ZooManagement;

/**
 * Zoo - Represents a zoo with animal management capabilities
 * @author ashongtical
 */
public class Zoo {

    public String name;          // The name of the zoo
    public Animal[] animals;     // Animal array
    private int counter;         // Tracks the number of animals in the zoo

    // Constructor
    /**
     * Constructor for the Zoo class.
     * Initializes the zoo with a given name and an empty array to store animals.
     *
     * @param name The name of the zoo
     */
    public Zoo(String name) {
        this.name = name;
        this.animals = new Animal[10];
        this.counter = 0;
    }

    // Method: Add an animal to the zoo
    /**
     * Adds an animal to the zoo.
     * If the zoo is not full, the animal is added to the array and the counter is incremented.
     *
     * @param animal The animal to be added to the zoo
     */
    public void addAnimal(Animal animal) {
        if (counter < 10) {
            animals[counter] = animal;
            counter++;
            System.out.println("Animal '" + animal.name + "' added to " + name);
        } else {
            System.out.println("Zoo is full!");
        }
    }

    /**
     * Removes an animal from the zoo by name.
     * If the animal is found, it is removed, and the array is shifted to fill the gap.
     *
     * @param animalName The name of the animal to be removed
     * @return true if the animal was successfully removed, false otherwise
     */
    public boolean deleteAnimal(String animalName) {
        for (int i = 0; i < counter; i++) {
            if (animals[i].name.equals(animalName)) {
                // Shift all elements after the found animal one position left
                for (int j = i; j < counter - 1; j++) {
                    animals[j] = animals[j + 1];
                }
                animals[counter - 1] = null; // Clear the last reference
                counter--;
                System.out.println("Animal '" + animalName + "' removed from " + name);
                return true;
            }
        }
        System.out.println("Can't find animal: " + animalName);
        return false;
    }

    // Method: Find an animal by name
    /**
     * Finds an animal by name and returns its index in the array.
     *
     * @param animalName The name of the animal to be found
     * @return The index of the animal if found, -1 otherwise
     */
    public int findAnimal(String animalName) {
        for (int i = 0; i < counter; i++) {
            if (animals[i].name.equals(animalName)) {
                return i;
            }
        }
        return -1;
    }

    // Method: Move an animal from one zoo to another
    /**
     * Moves an animal from this zoo to another zoo.
     * If the animal is found and the destination zoo is not full, the animal is moved.
     *
     * @param animalName The name of the animal to be moved
     * @param toZoo      The destination zoo
     * @param logistics  The logistics details for the transfer
     */
    public void moveAnimal(String animalName, Zoo toZoo, Logistics logistics) {
        int index = findAnimal(animalName);
        if (index != -1) {
            if (toZoo.getCounter() >= 10) {
                System.out.println("Cannot move animal - destination zoo is full!");
                return;
            }

            Animal animal = animals[index];
            toZoo.addAnimal(animal); // Add the animal to the target zoo
            deleteAnimal(animalName); // Remove the animal from the current zoo
            System.out.println("Animal '" + animalName + "' successfully moved from " + name + " Zoo to " + toZoo.getName() + " Zoo");
            System.out.println("Logistics details:");
            logistics.display(name, toZoo.getName(), animal); // Print logistics information
        } else {
            System.out.println("Can't find animal: " + animalName);
        }
    }

    // Method: Print detailed information of all animals in the zoo
    /**
     * Prints detailed information of all animals in the zoo.
     */
    public void displayAnimals() {
        System.out.println("----- Animals in " + name + " Zoo (" + getCounter() + "/10) -----");
        if (counter == 0) {
            System.out.println("There are no animals in this zoo.");
        } else {
            for (int i = 0; i < counter; i++) {
                System.out.println(i + 1 + ". Animal Name: " + animals[i].name +
                        ", Species: " + animals[i].species +
                        ", Age: " + animals[i].age);
            }
        }
        System.out.println("---------------------------------------------");
    }

    // Getter methods
    /**
     * Getter method for the zoo's name.
     *
     * @return The name of the zoo
     */
    public String getName() {
        return name;
    }

    /**
     * Getter method for the counter.
     *
     * @return The number of animals currently in the zoo
     */
    public int getCounter() {
        return counter;
    }


    /**
     * Getter method for an animal at a specific index.
     *
     * @param index The index of the animal to be retrieved
     * @return The animal at the specified index, or null if the index is out of bounds
     */
    public Animal getAnimal(int index) {
        if (index >= 0 && index < counter) {
            return animals[index];
        }
        return null;
    }

    /**
     * Getter method for an animal by name.
     *
     * @param animalName The name of the animal to be retrieved
     * @return The animal with the specified name, or null if not found
     */
    public Animal getAnimalByName(String animalName) {
        int index = findAnimal(animalName);
        return (index != -1) ? animals[index] : null;
    }
}